const jwt = require('jsonwebtoken');
const User = require('../models/user.model');

const auth = async (req) => {
    const authHeader = req.headers.authorization;
    if (!authHeader) {
        return null;
    }

    try {
        const token = authHeader.replace('Bearer ', '');
        const decoded = jwt.verify(token, process.env.JWT_SECRET);
        const user = await User.findById(decoded.userId);
        return user;
    } catch (err) {
        return null;
    }
};

module.exports = auth; 